<template>
	<el-form label-width="140px">
		<el-form-item v-for="(item, index) in extendInfo" :key="item.id" :label="item.col_title">
			<el-input v-if="item.form_type == 1" v-model="item.colValue" :placeholder="'请输入' + item.col_title"></el-input>
			<el-input v-else-if="item.form_type == 2" v-model="item.colValue" :placeholder="'请输入' + item.col_title" type="textarea" :autosize="{ minRows: 5, maxRows: 7 }"></el-input>
			<fortress-wang-editor v-else-if="item.form_type == 3" v-model="item.colValue"></fortress-wang-editor>
			<el-select v-else-if="item.form_type == 4" v-model="item.colValue" :placeholder="'请选择' + item.col_title" clearable>
				<el-option v-for="(item, index) in item.list" :key="index" :label="item.name" :value="item.code"></el-option>
			</el-select>
			<el-radio-group v-else-if="item.form_type == 5" v-model="item.colValue"></el-radio-group>
			<el-checkbox-group v-else-if="item.form_type == 6" v-model="item.colValue"></el-checkbox-group>
			<el-date-picker v-else-if="item.form_type == 7" v-model="item.colValue" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
			<el-date-picker v-else-if="item.form_type == 8" v-model="item.colValue" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
			<!-- <fortress-upload v-else-if="item.form_type == 9" :formData="item" @update:formData="item.colValue = $event.colValue" name="colValue" type="image"></fortress-upload> -->
			<picture-upload v-else-if="item.form_type == 9" type="picture-card" v-model="item.colValue" :limit="1"></picture-upload>

			<fortress-upload v-else-if="item.form_type == 10" :formData="item" @update:formData="item.colValue = $event.colValue" name="colValue" type="file"></fortress-upload>
			<fortress-upload v-else-if="item.form_type == 11" :formData="item" @update:formData="item.colValue = $event.colValue" name="colValue" type="video"></fortress-upload>
			<el-switch v-else-if="item.form_type == 12" v-model="item.colValue"></el-switch>
			<el-input-number v-else-if="item.form_type == 13" v-model="item.colValue"></el-input-number>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" @click="onSubmit">保存</el-button>
		</el-form-item>
	</el-form>
</template>

<script>
import request from "@/common/request"
import { enumList, urlList, dictList } from "@/api/public.api.js"
import pictureUpload from "@/components/fortress-upload/picture-upload.vue"

export default {
	name: "member-edit-extend",
	props: {
		id: {
			type: [Number, String],
			default: "",
		},
	},
	data() {
		return {
			extendInfo: [],
		}
	},
	computed: {},
	created() {
		this.getExtendInfo()
	},
	methods: {
		async getExtendInfo() {
			try {
				const { code, data, msg } = await request("get", "/member/member/extendInfo", { id: this.id })
				if (code != 0) {
					throw new Error(msg)
				}

				data.forEach(item => {
					if (item.form_type == 4) {
						if (!item.data_config) {
							return
						}
						const dataConfig = JSON.parse(item.data_config)

						item.list = []

						switch (+item.data_type) {
							case 1:
								this.getDict(item, dataConfig.dict)
								break
							case 2:
								this.getEnum(item, dataConfig.enumObj)
								break
							case 3:
								this.getUrlList(item, dataConfig.urlObj)
								break
							case 4:
								item.list = JSON.parse(dataConfig.jsonObj)
								break
						}
					}
				})
				this.extendInfo = data
			} catch (error) {
				this.$message.error(error.message)
			}
		},
		async getDict(item, dictname) {
			try {
				const { code, data, msg } = await dictList(dictname)
				if (code != 0) {
					throw new Error(msg)
				}
				item.list = data
			} catch (error) {
				this.$message.error(error.message)
			}
		},
		async getEnum(item, { enumName, moduleName }) {
			try {
				const { code, data, msg } = await enumList(enumName, moduleName)
				if (code != 0) {
					throw new Error(msg)
				}
				item.list = data
			} catch (error) {
				this.$message.error(error.message)
			}
		},
		async getUrlList(item, { url, code, name }) {
			try {
				const { code: resultCode, data, msg } = await urlList(url)
				if (resultCode != 0) {
					throw new Error(msg)
				}
				item.list = data.map(item => {
					return {
						code: item[code],
						name: item[name],
					}
				})
			} catch (error) {
				this.$message.error(error.message)
			}
		},
		async onSubmit() {
			try {
				const formData = {}
				this.extendInfo.map(item => {
					formData[item.col_name] = item.colValue
				})
				const { code, data, msg } = await request("post", "/member/memberExtendFieldConfig/editExtend?memberId=" + this.id, formData)
				if (code != 0) {
					throw new Error(msg)
				}
				this.$message.success("保存成功")
			} catch (error) {
				this.$message.error(error.message)
			}
		},
	},
	components: {
		pictureUpload,
	},
}
</script>

<style lang="scss" scoped>
.member-edit-extend {
}
</style>
